﻿using MiniIOC.IOCLIbs;
using MiniIOC.ServiceSample;

using IServiceProvider = MiniIOC.IOCLIbs.IServiceProvider;

IServiceCollection services = new ServiceCollection();

services.AddTransient<ITransientService, TransientService>();
services.AddScoped<IScopedService, ScopedService>();
services.AddSington<ISingletonService, SingletonService>();
services.AddTransient<ICustomerService, CustomerService>();
services.AddTransient(typeof(IGenericService<>), typeof(GenericService<>));

Console.WriteLine("\n================================\n");
using IServiceProvider serviceProvider = services.BuildServiceProvider();

#region 根容器比较
Console.WriteLine("根容器比较");
ISingletonService? singletonService1 = serviceProvider.GetService<ISingletonService>();
IScopedService? scopedService1 = serviceProvider.GetService<IScopedService>();
ITransientService? transientService1 = serviceProvider.GetService<ITransientService>();

ISingletonService? singletonService2 = serviceProvider.GetService<ISingletonService>();
IScopedService? scopedService2 = serviceProvider.GetService<IScopedService>();
ITransientService? transientService2 = serviceProvider.GetService<ITransientService>();

Console.WriteLine(singletonService1 == singletonService2);
Console.WriteLine(scopedService1 == scopedService2);
Console.WriteLine(transientService1 != transientService2);
#endregion

Console.WriteLine("\n================================\n");
using IScopedServiceProvider scopedServiceProvider = serviceProvider.CreateScope();

#region 子容器比较
Console.WriteLine("子容器比较");
ISingletonService? singletonService3 = scopedServiceProvider.ServiceProvider.GetService<ISingletonService>();
IScopedService? scopedService3 = scopedServiceProvider.ServiceProvider.GetService<IScopedService>();
ITransientService? transientService3 = scopedServiceProvider.ServiceProvider.GetService<ITransientService>();

ISingletonService? singletonService4 = scopedServiceProvider.ServiceProvider.GetService<ISingletonService>();
IScopedService? scopedService4 = scopedServiceProvider.ServiceProvider.GetService<IScopedService>();
ITransientService? transientService4 = scopedServiceProvider.ServiceProvider.GetService<ITransientService>();

Console.WriteLine(singletonService3 == singletonService4);
Console.WriteLine(scopedService3 == scopedService4);
Console.WriteLine(transientService3 != transientService4);
#endregion

Console.WriteLine("\n================================\n");

#region 根容器与子容器比较
Console.WriteLine("根容器与子容器比较");
Console.WriteLine(singletonService1 == singletonService3);
Console.WriteLine(scopedService1 != scopedService3);
Console.WriteLine(transientService1 != transientService3);
#endregion

Console.WriteLine("\n================================\n");

#region 构造函数注入与属性注入
Console.WriteLine("构造函数注入与属性注入");
ICustomerService? customer = scopedServiceProvider.ServiceProvider.GetService<ICustomerService>();
Console.WriteLine(customer != null);
Console.WriteLine(customer?.SingletonService == customer?.SingletonServiceForPropertyInject);
Console.WriteLine(customer?.ScopedService == customer?.ScopedServiceForPropertyInject);
Console.WriteLine(customer?.TransientService != customer?.TransientServiceForPropertyInject);
#endregion

Console.WriteLine("\n================================\n");

#region 泛型
Console.WriteLine("泛型构造函数注入与属性注入（支持注入同级别的 IServiceProvider）");
IGenericService<ITransientService>? genericService = serviceProvider.GetService<IGenericService<ITransientService>>();
Console.WriteLine(genericService != null);
Console.WriteLine(genericService?.ServiceProvider == serviceProvider);
Console.WriteLine(genericService?.ServiceProvider == genericService?.ServiceProviderForPropertyInject);
Console.WriteLine(genericService?.NoneInjectService == null);

IGenericService<IScopedService>? genericService2 = scopedServiceProvider.ServiceProvider.GetService<IGenericService<IScopedService>>();
Console.WriteLine(genericService2 != null);
Console.WriteLine(genericService2?.ServiceProvider == scopedServiceProvider);
Console.WriteLine(genericService2?.ServiceProvider == genericService2?.ServiceProviderForPropertyInject);
Console.WriteLine(genericService2?.NoneInjectService == null);
#endregion

Console.WriteLine("\n================================\n");

// https://github.com/dotnet/runtime
